// pages/personal/complaint/complaint.js
var utils = require('../../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        images:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id:options.id
        })
    },
    //图片上传
    uploadImg: function (e) {
        var that = this;
        var info = that.data.info;
        var index = e.currentTarget.dataset.index;
        utils.showLoading();
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function (result) {
                        utils.hideLoading();
                        var res = result.data;
                        //do something
                        //上传图片后返回的是字符串，需json.prise一下
                        var res = JSON.parse(res);
                         var imagesAll=that.data.images||[];
                        imagesAll.push(res)
                        that.setData({
                            images: imagesAll,
                        });
                        console.log(that.data.images)
                    },
                    fail: function (res) {
                        utils.hideLoading();
                        utils.message('上传服务器上传失败,请稍后再试2');
                        console.log(res);
                    }
                })
            }
        })
    },
    //删除图片
    onCloseVideoTap: function (e) {
        var that = this;
        var ind = e.currentTarget.dataset.ind;
        for (var j in that.data.images) {
            if (that.data.images[j] == that.data.images[ind]) {
                that.data.images.splice(j, 1)
                }
            }
            that.setData({
                images: that.data.images
            })
          console.log(that.data.images)
    },

    //提交
    formSubmit:function(e){
        var that=this;
        var imagesPost=[];
        for(var i in that.data.images){
            imagesPost.push(that.data.images[i].id)
        }
        if (e.detail.value.textarea != "" && imagesPost.length>0){
            utils.http("/api/v1/order/complaint_order",{
                id:that.data.id,
                images: imagesPost.join(","),
                content: e.detail.value.textarea
            },function(res){
                if (res.state==1){
                    wx.showToast({
                        title: '投诉成功',
                    })
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1,
                        })
                    },1000)
                }
            })
        }else{
            if (imagesPost.length==0){
                wx.showToast({
                    title: '请上传投诉图片',
                    icon: "none"
                })
            }
            if (e.detail.value.textarea == "") {
                wx.showToast({
                    title: '请输入投诉内容',
                    icon: "none"
                })
            }
            
            
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})